-- 纯函数
-- 

RedPointHandler = {}
-- key funcid 和字符串
RedPointConstant = {
    Bag_NewItem = "Bag_NewItem",       -- 背包tab红点
    Clothes_1 = "Clothes_1",  --时装主手
    Clothes_2 = "Clothes_2",  --时装副手
    Clothes_3 = "Clothes_3",  --时装北部
    Clothes_4 = "Clothes_4",  --时装玩具
    Clothes_5 = "Clothes_5",  --时装衣服
    Clothes_6 = "Clothes_6",  --时装头部

    Bag_PvpBox = "Bag_PvpBox",
    Bag_EventBox = "Bag_EventBox",
    Bag_NewCharm = "Bag_NewCharm",
    CorePorp_Add = "CorePorp_Add",
    CorePorp_Exchange = "CorePorp_Exchange",
    RecastMonster_Exchange = "RecastMonster_Exchange", --强化大师

    Pvp_PvpBox = "Pvp_PvpBox",
    Pvp_ChampionBox = "Pvp_ChampionBox",

    ActivityDungeon_EventBox = "ActivityDungeon_EventBox",

    Dungeon_DungeonBox = "Dungeon_DungeonBox",
    Dungeon_ExpCard = "Dungeon_ExpCard",

    Skill_Skill = "Skill_Skill",
    Skill_Talent = "Skill_Talent",
    Skill_Rune = "Skill_Rune",
    Daily = "Daily",

    Daily_DungeonBox = "Daily_DungeonBox",
    Daily_PvpBox = "Daily_PvpBox",
    Daily_EventBox = "Daily_EventBox",

    Mount_Ride = "Mount_Ride",

    CSGuildWar_Challenge = "CSGuildWar_Challenge",
}


--初始化红点
function RedPointHandler.init()
    RedPointHandler.isInit = true

     -- 角色
    local redPoint = UD.redPoint
    local createFunc = redPoint.newRedPointCheckNode
    createFunc(redPoint, Constant.FUNC_TYPE_PACK, nil)
    createFunc(redPoint, RedPointConstant.Bag_NewItem, Constant.FUNC_TYPE_PACK, RedPointHandler.checkItemNew)
    --createFunc(redPoint, Constant.FUNC_TYPE_PACK_TREASURE, Constant.FUNC_TYPE_PACK, nil) -- 背包中宝物界面隐藏，红点也需要先隐藏
    --createFunc(redPoint, RedPointConstant.Bag_PvpBox, Constant.FUNC_TYPE_PACK_TREASURE, RedPointHandler.checkPvpBox)
    --createFunc(redPoint, RedPointConstant.Bag_EventBox, Constant.FUNC_TYPE_PACK_TREASURE, RedPointHandler.checkEventBox)
    --createFunc(redPoint, RedPointConstant.Bag_NewCharm, Constant.FUNC_TYPE_PACK_TREASURE, RedPointHandler.checkNewCharm) -- 好运钥匙先取消

    createFunc(redPoint, Constant.FUNC_TYPE_PACK_WARDROBE, Constant.FUNC_TYPE_PACK)
    createFunc(redPoint, RedPointConstant.Clothes_1, Constant.FUNC_TYPE_PACK_WARDROBE, RedPointHandler.checkClothesMainHand)
    createFunc(redPoint, RedPointConstant.Clothes_2, Constant.FUNC_TYPE_PACK_WARDROBE, RedPointHandler.checkClothesOffHand)
    createFunc(redPoint, RedPointConstant.Clothes_3, Constant.FUNC_TYPE_PACK_WARDROBE, RedPointHandler.checkClothesBack)
    createFunc(redPoint, RedPointConstant.Clothes_4, Constant.FUNC_TYPE_PACK_WARDROBE, RedPointHandler.checkClothesToy)
    createFunc(redPoint, RedPointConstant.Clothes_5, Constant.FUNC_TYPE_PACK_WARDROBE, RedPointHandler.checkClothesCloth)
    createFunc(redPoint, RedPointConstant.Clothes_6, Constant.FUNC_TYPE_PACK_WARDROBE, RedPointHandler.checkClothesHead)

    createFunc(redPoint, Constant.FUNC_TYPE_PACK_TITLE, Constant.FUNC_TYPE_PACK, RedPointHandler.checkNewTitle)
    createFunc(redPoint, Constant.FUNC_TYPE_PACK_PRESTIGE, Constant.FUNC_TYPE_PACK, RedPointHandler.checkNewRenown)
    createFunc(redPoint, Constant.FUNC_TYPE_CORE_PROP, Constant.FUNC_TYPE_PACK,nil, nil, true)
    createFunc(redPoint, RedPointConstant.CorePorp_Add, Constant.FUNC_TYPE_CORE_PROP, RedPointHandler.checkCorePorpCanAdd, nil, true)
    createFunc(redPoint, RedPointConstant.CorePorp_Exchange, Constant.FUNC_TYPE_CORE_PROP, RedPointHandler.checkCorePropExchange, nil, true)
    createFunc(redPoint, RedPointConstant.RecastMonster_Exchange, Constant.FUNC_TYPE_PACK)
    redPoint:initByKey(Constant.FUNC_TYPE_PACK)

    -- 地下城
    createFunc(redPoint, Constant.FUNC_TYPE_DUNGEONS, nil)
    createFunc(redPoint, RedPointConstant.Dungeon_DungeonBox, Constant.FUNC_TYPE_DUNGEONS, RedPointHandler.checkDungeonBox)
    -- createFunc(redPoint, RedPointConstant.Dungeon_ExpCard, Constant.FUNC_TYPE_DUNGEONS, RedPointHandler.checkDungeonExpCard)
    redPoint:initByKey(Constant.FUNC_TYPE_DUNGEONS)

    -- 竞标赛
    createFunc(redPoint, Constant.FUNC_TYPE_PVP, nil)
    createFunc(redPoint, RedPointConstant.Pvp_PvpBox, Constant.FUNC_TYPE_PVP, RedPointHandler.checkPvpBox)
    createFunc(redPoint, RedPointConstant.Pvp_ChampionBox, Constant.FUNC_TYPE_PVP, RedPointHandler.checkChampionBox)

    redPoint:initByKey(Constant.FUNC_TYPE_PVP)

    -- 活动副本
    createFunc(redPoint, Constant.FUNC_TYPE_ACTIVITY_DUNGEONS, nil)
    createFunc(redPoint, RedPointConstant.ActivityDungeon_EventBox, Constant.FUNC_TYPE_ACTIVITY_DUNGEONS, RedPointHandler.checkEventBox)
    redPoint:initByKey(Constant.FUNC_TYPE_ACTIVITY_DUNGEONS)

    --制作
    createFunc(redPoint, Constant.FUNC_TYPE_COMPOUND, nil, RedPointHandler.checkMakeFormula)
    redPoint:initByKey(Constant.FUNC_TYPE_COMPOUND)

    --坐骑
    createFunc(redPoint, Constant.FUNC_TYPE_MOUNT, nil)
    createFunc(redPoint, RedPointConstant.Mount_Ride, Constant.FUNC_TYPE_MOUNT, RedPointHandler.checkMountRide)
    createFunc(redPoint, Constant.FUNC_TYPE_MOUNT_PIT, Constant.FUNC_TYPE_MOUNT, RedPointHandler.checkMountPit)
    redPoint:initByKey(Constant.FUNC_TYPE_MOUNT)

    --技能
    createFunc(redPoint, Constant.FUNC_TYPE_SKILL, nil)
    createFunc(redPoint, RedPointConstant.Skill_Skill, Constant.FUNC_TYPE_SKILL, RedPointHandler.checkSkill)
    createFunc(redPoint, RedPointConstant.Skill_Talent, Constant.FUNC_TYPE_SKILL, RedPointHandler.checkTalent)
    createFunc(redPoint, RedPointConstant.Skill_Rune, Constant.FUNC_TYPE_SKILL, RedPointHandler.checkRune)
    redPoint:initByKey(Constant.FUNC_TYPE_SKILL)

    --日常

    createFunc(redPoint, Constant.FUNC_TYPE_DAILY, nil)
    createFunc(redPoint, Constant.FUNC_TYPE_DAILY_RARE, Constant.FUNC_TYPE_DAILY, nil)
    createFunc(redPoint, RedPointConstant.Daily_DungeonBox, Constant.FUNC_TYPE_DAILY_RARE, RedPointHandler.checkDungeonBox)
    createFunc(redPoint, RedPointConstant.Daily_PvpBox, Constant.FUNC_TYPE_DAILY_RARE, RedPointHandler.checkPvpBox)
    createFunc(redPoint, RedPointConstant.Daily_EventBox, Constant.FUNC_TYPE_DAILY_RARE, RedPointHandler.checkEventBox)
    createFunc(redPoint, Constant.FUNC_TYPE_DAILY_TASK, Constant.FUNC_TYPE_DAILY, RedPointHandler.checkDailyTask)
    createFunc(redPoint, Constant.FUNC_TYPE_DAILY_WISH_WORLD, Constant.FUNC_TYPE_DAILY, RedPointHandler.checkDailyWishWorld)
    redPoint:initByKey(Constant.FUNC_TYPE_DAILY)

    --跨服工会战
    createFunc(redPoint, Constant.FUNC_TYPE_CROSSSERVER, nil)
    createFunc(redPoint, Constant.FUNC_TYPE_CS_GUILD_WAR, Constant.FUNC_TYPE_CROSSSERVER, nil)
    createFunc(redPoint,  RedPointConstant.CSGuildWar_Challenge, Constant.FUNC_TYPE_CS_GUILD_WAR, RedPointHandler.checkCSGuildWarChallenge, true)
    redPoint:initByKey(Constant.FUNC_TYPE_CROSSSERVER)
    
    RedPointHandler.isInit = false

end

function RedPointHandler.checkItemNew()
    if UIHandler:isFunctionOpen(Constant.FUNC_TYPE_PACK) then
        local items = UD:getItems()
        for k,item in pairs(items) do
            if item.isNew then
                return true
            end
        end
    end
    return false
end

function RedPointHandler.checkNewCharm()
    return UD:isCharmNumNew()
end

function RedPointHandler.checkDungeonExpCard()
    --地城经验卡
    local dungeonsRewardInfo = UD:getUserExpCard()
    if dungeonsRewardInfo then
        for i = 1, 3 do
            local activeStatus = dungeonsRewardInfo:getActiveStatusByLevel( i )
            local cumulationInfo = dungeonsRewardInfo:getCumulationInfoByLevel( i )
            if activeStatus and cumulationInfo > 0 then
                return true
            end
        end
    end
    return false
end

function RedPointHandler.checkDungeonBox()
    -- 地城宝箱
    local remainDayTimes = UD:getLuckBoxRestTimesByType(Enums.BoxType.dungeon)
    local remainWeekTimes = UD:getLuckBoxWeeklyRestTimesByType(Enums.BoxType.dungeon)
    if remainDayTimes > 0 and remainWeekTimes > 0 then --有剩余每日和每周开启次数
        local haveNum2 = UD:getDungeonLuckBox()
        if haveNum2 > 0 then
            return true
        end
    end
    return false
end

function RedPointHandler:checkPvpBox()
    --[[ local ownNum, usedToday = UD.player:getCharmUsedInfo()
	local canUsedToday = tonumber( GD:getSystemPar( "charmdailymaxcostamount" ) ) -- 今日可使用上限
	local canUseNum = canUsedToday - usedToday
	
    if canUseNum <= 0 then
		return false
	end ]]
	-- 竞技宝箱
    local remainDayTimes = UD:getLuckBoxRestTimesByType(Enums.BoxType.pvp)
    local remainWeekTimes = UD:getLuckBoxWeeklyRestTimesByType(Enums.BoxType.pvp)
	if remainDayTimes > 0 and remainWeekTimes > 0 then
		local haveNum2 = UD:getArenaLuckBox()
        if haveNum2 > 0 then
			return true
		end
    end
    return false
end

function RedPointHandler:checkChampionBox()
    local championInfo = app:sendMsg("PvpModel", "getChampionInfo")
    if championInfo then
        if championInfo.awardPartCount > 0 or championInfo.awardWonCount > 0 then
            return true
        end
    end
    return false
end

function RedPointHandler:checkEventBox()
    --[[ local ownNum, usedToday = UD.player:getCharmUsedInfo()
	local canUsedToday = tonumber( GD:getSystemPar( "charmdailymaxcostamount" ) ) -- 今日可使用上限
	local canUseNum = canUsedToday - usedToday
	
    if canUseNum <= 0 then
		return false
	end ]]
	-- 活动宝箱
	local remainDayTimes = UD:getLuckBoxRestTimesByType(Enums.BoxType.eventBox)
	local remainWeekTimes = UD:getLuckBoxWeeklyRestTimesByType(Enums.BoxType.eventBox)
	if remainDayTimes > 0 and remainWeekTimes > 0 then
		local haveNum2 = UD:getEventLuckBox()
        if haveNum2 > 0 then
			return true
		end
    end
    return false
end

function RedPointHandler.checkClothesMainHand()
    return UD:getClothesNewByIdx(1)
end
function RedPointHandler.checkClothesOffHand()
    return UD:getClothesNewByIdx(2)
end
function RedPointHandler.checkClothesBack()
    return UD:getClothesNewByIdx(3)
end
function RedPointHandler.checkClothesToy()
    return UD:getClothesNewByIdx(4)
end
function RedPointHandler.checkClothesCloth()
    return UD:getClothesNewByIdx(5)
end
function RedPointHandler.checkClothesHead()
    return UD:getClothesNewByIdx(6)
end

function RedPointHandler.checkNewTitle(node)
    if UIHandler:isFunctionOpen(Constant.FUNC_TYPE_PACK_TITLE) then
        return UD:hasNewGotTitle()
    end
    return false
end

function RedPointHandler.checkNewRenown(node)
    if UIHandler:isFunctionOpen(Constant.FUNC_TYPE_PACK_PRESTIGE) then
        return UD:isNewGotRenownExist()
    end
    return false
end

function RedPointHandler.checkMountRide()
    -- 新获得的坐骑 or 坐骑可以合成
    local mounts = app:sendMsg("MountModel", "getMountDatas")
    for i,mount in ipairs(mounts) do
        if mount.isNew or mount:checkComposition() then
            return true
        end
    end
    return false
end

function RedPointHandler.checkMountPit()
    -- 1.新获得的坐骑 or 坐骑有操作 or 可以合成 or 可以激活
    local mounts = app:sendMsg("MountModel", "getMountDatas")
    for i,mount in ipairs(mounts) do
        if mount.isNew or 
            mount:checkComposition() or
            mount:checkCanOperation() or
            mount:checkActiveOp() then
            return true
        end
    end
    return false
end

--判断技能
function RedPointHandler.checkSkill()
   return UD:hasSkillCanUpgrade()
end

--判断天赋
function RedPointHandler.checkTalent()
    local dotShow = false
    if UD:getTalentPoint() > 0 then -- 存在剩余天赋点
        for k,talent in pairs( UD:getSpecTalents() ) do
            if talent:canActive() then -- 未激活 && 可激活
                dotShow = true
                break
            end
        end
    end
    return dotShow
end

--判断符文
function RedPointHandler.checkRune()
    if UD:isNewGetRunesExist() then
        return true
    end

    -- 初始化的时候初始化一次，后面不再考虑此条件
    if RedPointHandler.isInit then
        return UD:isSpecRunesHasOp()
    end

    return false
end

function RedPointHandler.checkMakeFormula()
    return UD:hasNewGetFormulaId()
end

-- 核心属性 红点计算
function RedPointHandler.checkCorePorpCanAdd()
    if not UIHandler:isFunctionOpen(Constant.FUNC_TYPE_CORE_PROP) then
        return false
    end

    local CorePropUIHelp = require("app.coreProp.CorePropUIHelp")
    
    local isAllMaxLevel = true
    local values = UD.coreProp:getValues()
    for k, v in pairs(values) do
        local maxLevel = CorePropUIHelp.getMaxAddLevel(k) or 0
        if v < maxLevel then
            isAllMaxLevel = false
            break
        end
    end
    if isAllMaxLevel then
        return false
    end

    -- 有剩余点数没加
    local leftPoint = UD.coreProp:getLeftPoint()
    return leftPoint > 0
end


function RedPointHandler.checkCorePropExchange()
    if not UIHandler:isFunctionOpen(Constant.FUNC_TYPE_CORE_PROP) then
        return false
    end
    local CorePropUIHelp = require("app.coreProp.CorePropUIHelp")
    
    local isAllMaxLevel = true
   
    local values = UD.coreProp:getValues()
    for k, v in pairs(values) do
        local maxLevel = CorePropUIHelp.getMaxAddLevel(k) or 0
        if v < maxLevel then
            isAllMaxLevel = false
            break
        end
    end
    if isAllMaxLevel then
        return false
    end

    -- 可以转换新的点数
    local maxSum = CorePropUIHelp.getMaxExchangePoint()
    local curSum = UD.coreProp:getSumPoint()
    if curSum >= maxSum then
        return false
    end

    local itemNum = CorePropUIHelp.getCorePropItemNum()
	local itemCost = CorePropUIHelp.getCorePropItemCost(curSum)
    return itemNum >= itemCost
end

--日常
function RedPointHandler.checkDailyTask()
    return app:sendMsg("DailyModel", "checkRedPoints", Constant.FUNC_TYPE_DAILY_TASK)
end

function RedPointHandler.checkDailyWishWorld()
    return app:sendMsg("DailyModel", "checkRedPoints", Constant.FUNC_TYPE_DAILY_WISH_WORLD)
end

function RedPointHandler.checkDailyRare()

end


--跨服工会战挑战 红点
function RedPointHandler.checkCSGuildWarChallenge()
    local CrossServerGuildWarHelper = require("app.crossServer.views.guildWar.CrossServerGuildWarHelper")
    local taskData = CrossServerGuildWarHelper.getTasks()
    for k, v in pairs(taskData) do
        local task = UD:getTaskById(v)
        if task and task:isFinished() then
            return true
        end
    end
    return false
end
    
-- 避免多次计算
function RedPointHandler.onRareChange()
    --新钥匙
    local trueOrFalse = false
    --[[ trueOrFalse = RedPointHandler.checkNewCharm() 
    UD.redPoint:setStateByKey(RedPointConstant.Bag_NewCharm, trueOrFalse) ]]
    
    -- dungeon box
    trueOrFalse = RedPointHandler.checkDungeonBox()
    UD.redPoint:setStateByKey(RedPointConstant.Dungeon_DungeonBox, trueOrFalse)
    UD.redPoint:setStateByKey(RedPointConstant.Daily_DungeonBox, trueOrFalse)

    -- pvp box
    trueOrFalse = RedPointHandler.checkPvpBox()
    UD.redPoint:setStateByKey(RedPointConstant.Daily_PvpBox, trueOrFalse)
    UD.redPoint:setStateByKey(RedPointConstant.Pvp_PvpBox, trueOrFalse)

    --event box
    trueOrFalse = RedPointHandler.checkEventBox()
    UD.redPoint:setStateByKey(RedPointConstant.Daily_EventBox, trueOrFalse)
    UD.redPoint:setStateByKey(RedPointConstant.ActivityDungeon_EventBox, trueOrFalse)
end

return RedPointHandler


